﻿Public Class CreatedCasesAdminForm

    Private Sub CreatedCasesAdminForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadCasesGrid()
    End Sub

    Private Sub LoadCasesGrid()
        With Me
            With .ExistingCasesGrid
                .DataSource = LoadDataView("Select DataCut_ID as [Case ID], Description, Notes, Budget_ID, Invisible From Case_Templates_IDs")

                .Refresh()

                .Columns("Notes").Visible = False
                .Columns("Budget_ID").Visible = False
                .Columns("Invisible").Visible = False

            End With
        End With


    End Sub

    Function CreateID() As String

        Dim d, m, y, mm, h, s, ms, t As String

        d = Now.Day
        m = Now.Month
        y = Now.Year

        If d.Length <> 2 Then
            d = "0" & d
        End If

        If m.Length <> 2 Then
            m = "0" & m
        End If

        mm = Now.Minute

        h = Now.Hour

        s = Now.Second

        ms = Now.Millisecond

        If mm.Length <> 2 Then
            mm = "0" & mm
        End If

        If h.Length <> 2 Then
            h = "0" & h
        End If

        If s.Length <> 2 Then
            s = "0" & s
        End If

        Select Case ms.Length
            Case 1
                ms = "000" & ms
            Case 2
                ms = "00" & ms
            Case 3
                ms = "0" & ms
        End Select

        t = "CASEIDNUMBER00000" & d & m & y & m & h & s & ms
        CreateID = t
    End Function

    Private Sub btnAddCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCase.Click
        '----DRAFT----- loads form, clears text boxes ok
        '**tick boxes still hold the values for the last form loaded, they need to be cleared**

        'clears text boxes ready for input

        With CreateCasesForm
            .txtIDBox.Text = CreateID()
            .txtCaseDesc.Text = ""
            .txtCaseNotes.Text = ""
            .txtIDBox.Enabled = False
            .InvisibleCaseCheck.Checked = False

            .LoadCaseFieldsBox()
            .LoadBudgetBox()
            .LoadDataCutBox()
            .TableNameBox.Enabled = True


            ''.Case_Fields_Box.ClearSelected() '-----not sure how to clear the check boxes
            ''.Case_Fields_Box.SelectedItems.Clear()
            ''.Case_Fields_Box.Items.Remove(.Case_Fields_Box.SelectedItems)


        End With

        If CreateCasesForm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim Sql As String
            Dim Invisible As Integer

            With CreateCasesForm

                If .InvisibleCaseCheck.Checked Then
                    Invisible = 1
                Else
                    Invisible = 0
                End If

                Sql = "insert into Case_Templates_IDs (DataCut_ID, Description, Notes, Budget_ID, Invisible) Values('" & .txtIDBox.Text & "','" & RemoveBadChars(.txtCaseDesc.Text) & "','" & RemoveBadChars(.txtCaseNotes.Text) & "', '" & .BudgetBox.SelectedValue.ToString & "', " & Invisible & ")"
                RunNonQuery(Sql)

                AddCheckedItems()

            End With

            LoadCasesGrid()


        End If
        CreateCasesForm.Close()
    End Sub

    Private Sub AddCheckedItems()
        Dim SQL As String

        With CreateCasesForm

            SQL = "insert into Case_Templates (DataCut_ID, Table_Name, DataCut_ID_Act, Table_Name_Act) Values('" & .txtIDBox.Text & "','" & .TableNameBox.SelectedValue.ToString & "', '" & .DataCutBox.SelectedValue.ToString & "', '" & .ActTableBox.SelectedValue.ToString & "')"
                RunNonQuery(SQL)

        End With
    End Sub

    Private Sub btnDeleteCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteCase.Click
        'Deletes the case records from the template and template_ID tables
        Dim Sql As String
        Dim DataCutID As String

        If ExistingCasesGrid.RowCount = 0 Then Exit Sub


        DataCutID = ExistingCasesGrid.Item(ExistingCasesGrid.Columns("Case ID").Index, ExistingCasesGrid.CurrentRow.Index).Value
        If MessageBox.Show("Are you sure you want to delete business case " & DataCutID & "?", "Delete Case?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Sql = "Delete from Case_Templates_IDs where DataCut_ID = '" & DataCutID & "'"

            'Calls the sub to delete the records from the case template table
            DeleteCaseFields()
            If RunNonQuery(Sql) = "" Then
                'Add activity Not currently working
                AddActivity(CurrentUser.Name, "Case " & DataCutID & " was deleted", 9, "")
                LoadCasesGrid()


            End If
        End If
    End Sub

    Private Sub DeleteCaseFields()
        'Deletes the various field records from the template table associated with the main case record
        Dim Sql As String
        Sql = "Delete from Case_Templates where DataCut_ID = '" & ExistingCasesGrid.Item(ExistingCasesGrid.Columns("Case ID").Index, ExistingCasesGrid.CurrentRow.Index).Value & "'"
        RunNonQuery(Sql)


    End Sub



    Private Sub btnEditCase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditCase.Click
        'Launches Create Cases Form to allow editing of business cases


        Dim Sql As String
        Dim TheData As DataView


        If ExistingCasesGrid.RowCount = 0 Then Exit Sub


        With CreateCasesForm
            .txtIDBox.Text = ExistingCasesGrid.Item(ExistingCasesGrid.Columns("Case ID").Index, ExistingCasesGrid.CurrentRow.Index).Value.ToString
            .txtCaseDesc.Text = ExistingCasesGrid.Item(ExistingCasesGrid.Columns("Description").Index, ExistingCasesGrid.CurrentRow.Index).Value.ToString
            .txtCaseNotes.Text = ExistingCasesGrid.Item(ExistingCasesGrid.Columns("Notes").Index, ExistingCasesGrid.CurrentRow.Index).Value.ToString

            If ExistingCasesGrid.Item(ExistingCasesGrid.Columns("Invisible").Index, ExistingCasesGrid.CurrentRow.Index).Value.ToString = "1" Then
                .InvisibleCaseCheck.Checked = True
            Else
                .InvisibleCaseCheck.Checked = False
            End If


            .txtIDBox.Enabled = False

            .LoadCaseFieldsBox()
            .LoadBudgetBox()
            .LoadDataCutBox()

            .BudgetBox.SelectedValue = ExistingCasesGrid.Item(ExistingCasesGrid.Columns("Budget_ID").Index, ExistingCasesGrid.CurrentRow.Index).Value.ToString

            Sql = "select * from dbo.Case_Templates where DataCut_ID = '" & .txtIDBox.Text & "'"
            TheData = LoadDataView(Sql)

            If TheData.Count <> 0 Then

                With CreateCasesForm
                    .TableNameBox.SelectedValue = TheData(0).Item("Table_Name").ToString
                    .ActTableBox.SelectedValue = TheData(0).Item("Table_Name_Act").ToString
                    .DataCutBox.SelectedValue = TheData(0).Item("DataCut_ID_Act").ToString
                End With

            End If

            .TableNameBox.Enabled = False


        End With

        If CreateCasesForm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim Invisible As Integer

            With CreateCasesForm

                If .InvisibleCaseCheck.Checked Then
                    Invisible = 1
                Else
                    Invisible = 0
                End If

                Sql = "Delete from Case_Templates_IDs where DataCut_ID = '" & .txtIDBox.Text & "'"
                RunNonQuery(Sql)

                Sql = "insert into Case_Templates_IDs (DataCut_ID, Description, Notes, Budget_ID, Invisible) Values('" & .txtIDBox.Text & "','" & RemoveBadChars(.txtCaseDesc.Text) & "','" & RemoveBadChars(.txtCaseNotes.Text) & "', '" & .BudgetBox.SelectedValue.ToString & "', " & Invisible & ")"
                RunNonQuery(Sql)

                Sql = "Delete from Case_Templates where DataCut_ID = '" & .txtIDBox.Text & "'"
                RunNonQuery(Sql)

                AddCheckedItems()

                'update the data table's description

                Sql = "update " & .TableNameBox.SelectedValue & " set Description = '" & RemoveBadChars(.txtCaseDesc.Text) & "' where datacut_id = '" & .txtIDBox.Text & "'"
                RunNonQuery(Sql)

            End With

            LoadCasesGrid()


        End If

        CreateCasesForm.Close()

    End Sub

    Private Sub ExistingCasesGrid_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ExistingCasesGrid.CellContentClick

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()

    End Sub
End Class